<!--
 * @Description: 云币明细
-->
<template>
  <div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" @keyup.enter.native="search()">
      <el-form-item label="标题">
        <el-input v-model="formInline.getUseDesc" style="width:200px" />
      </el-form-item>

      <div style="width:100%;display:flex;justify-content:flex-end;">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search()" size="mini">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="kong()" size="mini" icon="el-icon-delete">清空</el-button>
        </el-form-item>
      </div>
    </el-form>

    <el-table :data="tableData" v-loading="loading" border height="550px" style="width: 100%"
      :header-cell-style="{ background: '#eef1f6', color: '#606266' }">
      <el-table-column type="index" width="60" label="序号" align="center" />
      <el-table-column label="标题" align="center" prop="getUseDesc" />
      <el-table-column v-if="formInline.type == 1" label="云币类型" align="center">
        <template slot-scope="scope">
          {{ scope.row.isDirectedExchange == 1 ? '定向云币' : '普通云币' }}
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center">
        <template slot-scope="scope">
          <el-link :underline="false" :type="scope.row.bonuspoint < 0 ? 'danger' : 'success'">{{
            `${scope.row.bonuspoint < 0 ? '' : '+'}${scope.row.bonuspoint}` }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" prop="bonuspointTime" />
      <el-table-column label="操作" :align="'center'">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isDirectedExchange == 1 && scope.row.bonuspointSource == 104 && scope.row.bonuspoint > 0"
            type="text" size="mini" @click="withdrawHandle(scope.row)">撤回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="footer-wrapper">
      <el-button class="btn" @click="$router.go(-1)" size="mini" type="primary">返回</el-button>
      <div class="block">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 50, 100, 150, 200]" :current-page="listQuery.current" :page-size="listQuery.size"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { queryMemberConsumerIntegral, withdrawMemberIntegral } from '@/api/memberinfo';
export default {
  data() {
    return {
      formInline: {
        // 类型 0 会员赠送酒币/ 商家赠送云币
        type: 0,
        memberId: '',
        getUseDesc: '',
      },
      tableData: [],
      loading: false,
      listQuery: {
        current: 1,
        size: 20,
      },
      total: 0,
    };
  },
  created() {
    const loginInfo = localStorage.getItem('logininfo');
    const orgSeq = JSON.parse(loginInfo).orgSeq;
    this.formInline.memberId = this.$route.params.memberId;
    this.formInline.type = this.$route.params.type;
    if (this.formInline.memberId) {
      this.getList();
    }
  },
  methods: {
    search() {
      this.listQuery.current = 1;
      this.getList();
    },
    getList() {
      const data = {
        size: this.listQuery.size,
        current: this.listQuery.current,
        condition: {
          ...this.formInline,
        },
        ascs: [],
        descs: [],
      };

      if (this.formInline.type == 1) {
        data.condition.queryType = 0
      }

      queryMemberConsumerIntegral(data).then((res) => {
        this.total = res.total;
        this.tableData = res.records;
      });
    },
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.getList();
    },
    kong() {
      this.formInline = this.$options.data().formInline;
    },
    withdrawHandle(row) {
      this.$confirm('是否撤回?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true;
            instance.confirmButtonText = '提交中...';

            withdrawMemberIntegral({
              memberId: row.memberId,
              itemId: row.itemId
            }).then((res) => {
              if (res.code == 1) {
                this.$message.success(res.result.message || '撤回成功');
                this.getList();
              } else {
                this.$message.error(res.message || '撤回失败');
              }
            }).finally(() => {
              instance.confirmButtonLoading = false;
              done();
            })
          } else {
            done();
          }
        }
      })
    }
  },
};
</script>

<style lang="scss" scoped>
.footer-wrapper {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 10px;
}

.demo-form-inline {
  /deep/ .el-form-item__content {
    line-height: normal;
  }
}
</style>
